import Utils from "../utils/Utils";
import ResManager from "./ResManager";
import { LocalStorage } from "./storage/LocalStorage";

const MUSIC_PATH = "sounds/music/{0}";
const SOUND_PATH = "sounds/sound/{0}";
let Const = {
    SoundVol:'SoundVol',
    MusicVol:'MusicVol',
    MusicMute:'MusicMute',
    SoundMute:'SoundMute',
}
export class AudioManager {

    private music_id: number;
    private _music_volume: number;
    private _music_mute: boolean;

    private _sound_volume: number;
    private _sound_mute: boolean;
    private storage: LocalStorage;

    private constructor() {
        this.music_id = -1;
        this.storage = LocalStorage.instance;
    }

    private static _instance: AudioManager;
    public static get instance() {
        if (!this._instance) {
            this._instance = new AudioManager();
            this._instance.init();
        }
        return this._instance;
    }

    private init() {
        this.sound_volumn = this.storage.getFloat(Const.SoundVol, 0.5);
        this.music_volumn = this.storage.getFloat(Const.MusicVol, 0.5);
        this.music_mute = this.storage.getBool(Const.MusicMute, false);
        this.sound_mute = this.storage.getBool(Const.SoundMute, false);
    }
    public saveAudioConfig() {
        this.storage.setFloat(Const.SoundVol, this._sound_volume);
        this.storage.setFloat(Const.MusicVol, this._music_volume);
        this.storage.setBool(Const.MusicMute, this._music_mute);
        this.storage.setBool(Const.SoundMute, this._sound_mute);
    }
    public play_music(name: string, loop = true) {
        if (this.music_mute) {
            return;
        }
        if (this.music_id >= 0) {
            this.stop_music();
        }
        let path = Utils.strfmt(MUSIC_PATH, name);
        let clip = ResManager.instance.getRes(path, cc.AudioClip) as cc.AudioClip;
        cc.audioEngine.playMusic(clip, loop);
    }

    public play_sound(name: string, loop = false): number {
        if (this.sound_mute) {
            return;
        }
        let path = Utils.strfmt(SOUND_PATH, name);
        let clip = ResManager.instance.getRes(path, cc.AudioClip) as cc.AudioClip;
        return cc.audioEngine.playEffect(clip, loop);
    }
    public stop_music() {
        if (this.music_id < 0) {
            cc.log("no music is playing");
            return;
        }
        cc.audioEngine.stopMusic();
        this.music_id = -1;
    }
    public stop_sound(id: number) {
        cc.audioEngine.stopEffect(id);
    }
    public get music_mute() {
        return this._music_mute;
    }

    public set music_mute(is_mute: boolean) {
        this._music_mute = is_mute;
        if (this.music_id < 0) {
            return;
        }
        if (is_mute) {
            cc.audioEngine.pauseMusic();
        }
        else {
            cc.audioEngine.resumeMusic();
        }
    }
    public get sound_mute() {
        return this._sound_mute;
    }

    public set sound_mute(is_mute: boolean) {
        this._sound_mute = is_mute;
        //cc.audioEngine.ef
        if (is_mute) {
            cc.audioEngine.stopAllEffects();
        }
    }
    public get music_volumn() {
        return this._music_volume;
    }
    public set music_volumn(volume: number) {
        this._music_volume = volume;
        cc.audioEngine.setMusicVolume(volume);
    }
    public get sound_volumn() {
        return this._sound_volume;
    }
    public set sound_volumn(volume: number) {
        this._sound_volume = volume;
        cc.audioEngine.setEffectsVolume(volume);
    }

    public clear_cache() {
        cc.audioEngine.stopMusic();
        cc.audioEngine.stopAllEffects();
        cc.audioEngine.uncacheAll();
    }
}